package ics.zychen.sql;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class RenrenDataHelper extends ADataHelper{

   private static int DB_VERSION = 2;

   private RenrenSqliteHelper dbHelper;

   public RenrenDataHelper(Context context){
	   super(context);
       dbHelper=new RenrenSqliteHelper(context, DB_NAME, null, DB_VERSION);

       db= dbHelper.getWritableDatabase();

    }

   	
    public void Close()
    {

        db.close();

        dbHelper.close();

    }

    public List<UserInfo> GetUserList(Boolean isSimple)
    {

        List<UserInfo> userList = new ArrayList<UserInfo>();

        Cursor cursor=db.query(RenrenSqliteHelper.RENREN_TB_NAME, null, null, null, null, null, UserInfo.ID+" DESC");

        cursor.moveToFirst();

        while(!cursor.isAfterLast()&& (cursor.getString(1)!=null)){

            UserInfo user=new UserInfo();

            user.setId(cursor.getString(0));

            user.setUserId(cursor.getString(1));

            if(!isSimple){

	            user.setUserName(cursor.getString(2));
	
	            Bitmap icon= BitmapFactory.decodeByteArray(cursor.getBlob(3), 0, cursor.getBlob(3).length);
	
	            user.setUserIcon(icon);

            }

            userList.add(user);

            cursor.moveToNext();

        }

        cursor.close();

        return userList;

    }



    public UserInfo HaveUserInfo(String UserId)

    {

        Boolean b=false;

        Cursor cursor=db.query(RenrenSqliteHelper.RENREN_TB_NAME, null, UserInfo.USERID + "=" + UserId, null, null, null,null);

        b=cursor.moveToFirst();

        Log.e("RenrenDataHelper","HaveUserInfo:"+b.toString());
        
        if(!b) return null;
        
        UserInfo user = new UserInfo();
        user.setId(cursor.getString(0));

        user.setUserId(cursor.getString(1));

        if(cursor.getString(2) != null){
        	user.setUserName(cursor.getString(2));
        } else {
        	user.setUserName("");
        }

        if(cursor.getBlob(3) != null){
        	Bitmap icon= BitmapFactory.decodeByteArray(cursor.getBlob(3), 0, cursor.getBlob(3).length);

        	user.setUserIcon(icon);
        } else{
        	user.setUserIcon(null);
        }
        
        cursor.close();
        return user;

    }

    


    public int UpdateUserInfo(String userName,Bitmap userIcon,String UserId)

    {

        ContentValues values = new ContentValues();

        values.put(UserInfo.USERNAME, userName);


        final ByteArrayOutputStream os = new ByteArrayOutputStream();  

        userIcon.compress(Bitmap.CompressFormat.PNG, 100, os);   

        values.put(UserInfo.USERICON, os.toByteArray());

        int id= db.update(RenrenSqliteHelper.RENREN_TB_NAME, values, UserInfo.USERID + "=" + UserId, null);

        Log.e("UpdateUserInfo2",id+"");

        return id;

    }

    

    public int UpdateUserInfo(UserInfo user)

    {

        ContentValues values = new ContentValues();

        values.put(UserInfo.USERID, user.getUserId());

        values.put(UserInfo.USERNAME, user.getUserName());

        int id= db.update(RenrenSqliteHelper.RENREN_TB_NAME, values, UserInfo.USERID + "=" + user.getUserId(), null);

        Log.e("UpdateUserInfo",id+"");

        return id;

    }

    

    //���users��ļ�¼

    public Long SaveUserInfo(UserInfo user)

    {

        ContentValues values = new ContentValues();

        values.put(UserInfo.USERID, user.getUserId());

        values.put(UserInfo.USERNAME, user.getUserName());
        
        final ByteArrayOutputStream os = new ByteArrayOutputStream();  

        user.getUserIcon().compress(Bitmap.CompressFormat.PNG, 100, os);   

        values.put(UserInfo.USERICON, os.toByteArray());

        Long uid = db.insert(RenrenSqliteHelper.RENREN_TB_NAME, UserInfo.ID, values);

        Log.e("SaveUserInfo",uid+"");

        return uid;

    }

    

    //ɾ��users��ļ�¼

    public int DelUserInfo(String UserId){

        int id=  db.delete(RenrenSqliteHelper.RENREN_TB_NAME, UserInfo.USERID +"="+UserId, null);

        Log.e("DelUserInfo",id+"");

        return id;

    }

}
